package com.dp.mallchat.common.user.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dp.mallchat.common.chat.service.RoomService;
import com.dp.mallchat.common.common.domain.vo.req.CursorPageBaseReq;
import com.dp.mallchat.common.common.domain.vo.resp.CursorPageBaseResp;
import com.dp.mallchat.common.common.utils.CursorUtils;
import com.dp.mallchat.common.user.dao.UserDao;
import com.dp.mallchat.common.user.dao.UserFriendDao;
import com.dp.mallchat.common.user.domain.entity.UserApply;
import com.dp.mallchat.common.user.domain.entity.UserFriend;
import com.dp.mallchat.common.user.domain.user.entity.User;
import com.dp.mallchat.common.user.domain.user.resp.UserFriendResp;
import com.dp.mallchat.common.user.mapper.UserFriendMapper;
import com.dp.mallchat.common.user.service.UserFriendService;
import com.dp.mallchat.common.user.service.adapter.UserFriendAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: dupeng
 * @CreateTime: 2024-06-19  18:15
 * @Description:
 */
@Service
@Slf4j
public class UserFriendServiceImpl extends ServiceImpl<UserFriendMapper, UserFriend> implements UserFriendService {

    @Autowired
    private UserFriendDao userFriendDao;
    @Autowired
    private UserDao userDao;

    @Autowired
    private RoomService roomService;

    @Override
    public CursorPageBaseResp<UserFriendResp> friendList(Long uid, CursorPageBaseReq cursorPageBaseReq) {
        //查询好友
        CursorPageBaseResp<UserFriend> friendList = userFriendDao.getFriendPage(uid, cursorPageBaseReq);
        if (CollectionUtils.isEmpty(friendList.getList())) {
            return CursorPageBaseResp.empty();
        }
        //查询在线用户
        List<Long> uIds = friendList.getList().stream().map(UserFriend::getFriendUid).collect(Collectors.toList());
        List<User> userList = userDao.getActiveUser(uIds);
        //返回
        return CursorPageBaseResp.init(friendList, UserFriendAdapter.buildFriendList(friendList.getList(), userList));
    }


    @Override
    public UserFriend isUserFriend(Long uid, Long targetUid) {
        return userFriendDao.getFriendByUid(uid, targetUid);
    }

    @Override
    public List<UserFriend> isUserFriend(Long uid, List<Long> targetUids) {
        return userFriendDao.getFriendByUids(uid, targetUids);
    }

    @Override
    public void createFriend(Long uid, Long targetId) {
        //双向好有，后续通过uid即可查看我本人拥有哪些好友
        UserFriend userFriend1 = new UserFriend();
        userFriend1.setUid(uid);
        userFriend1.setFriendUid(targetId);
        UserFriend userFriend2 = new UserFriend();
        userFriend2.setUid(targetId);
        userFriend2.setFriendUid(uid);
        userFriendDao.saveBatch(Arrays.asList(userFriend1, userFriend2));
    }


    @Override
    public void deleteFriend(Long uid, Long friendUid) {
        //是否存在好友关系
        List<UserFriend> bothFriendByUid = userFriendDao.getBothFriendByUid(uid, friendUid);
        if (CollectionUtils.isEmpty(bothFriendByUid)) {
            log.info("没有好友关系：{},{}", uid, friendUid);
            return;
        }
        //删除好友(逻辑删除)
        List<Long> ids = bothFriendByUid.stream().map(UserFriend::getId).collect(Collectors.toList());
        userFriendDao.removeByIds(ids);

        //禁用房间
        roomService.disableFriendRoom(Arrays.asList(uid, friendUid));
    }
}
